﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simple.Repository.Models.System;

[Table("QHSE_XY_SYSUSER")]
//username必须唯一
[Index(nameof(UserName), IsUnique = true)]
public class SysUser : EntityBase
{
    /// <summary>
    /// 学号
    /// </summary>
    public SysUser() { }
    [Key]
    //编号
    public string Id { get; set; }
    //？？姓名
    public string Name { get; set; }
    //
    public string UserName { get; set; }
    public string Password { get; set; }
    public string? Phone { get; set; }
    public string? Email { get; set; }
    public GenderType Gender { get; set; }
    public bool IsEnabled { get; set; }

    public string? PositionId { get; set; }
    public string? OrganizationId { get; set; }
    public string? CreatedUserId { get; set; }
    public string? UpdatedUserId { get; set; }

    public AdminType AdminType { get; set; }
    public long RowVersion { get; set; }
    public bool IsDeleted { get; set; }
    public DateTime? CreatedTime { get; set; }
    public DateTime? UpdatedTime { get; set; }


    /// <summary>
    /// 用户角色
    /// </summary>
    public List<SysUserRole> UserRoles { get; set; } = new List<SysUserRole>();

    /// <summary>
    /// 用户数据范围
    /// </summary>
    public List<SysUserDataScope> UserDataScopes { get; set; } = default!;


    public void SetPassword(string password)
    {
        Password = password;
    }
    public override void ConfigureEntity(ModelBuilder builder)
    {

        // AdminType 默认值 3
        builder.Entity<SysUser>().Property(r => r.AdminType).HasDefaultValue(AdminType.None);
    }

    public void AddRole(params string[] roleIds)
    {
        foreach (var roleId in roleIds)
        {
            if (UserRoles.Any(ur => ur.RoleId == roleId))
            {
                continue;
            }

            var userRole = new SysUserRole()
            {
                UserId = Id,
                RoleId = roleId
            };
            UserRoles.Add(userRole);
        }
    }

    public void RemoveRole(params string[] roleIds)
    {
        foreach (var roleId in roleIds)
        {
            var userRole = UserRoles.FirstOrDefault(ur => ur.RoleId == roleId);
            if (userRole != null)
            {
                UserRoles.Remove(userRole);
            }
        }
    }

    public void SetRole(params string[] roleIds)
    {
        var oldRoleIds = UserRoles.Select(ur => ur.RoleId);

        var removeRoleIds = oldRoleIds.Except(roleIds);
        var addRoleIds = roleIds.Except(oldRoleIds);

        RemoveRole(removeRoleIds.ToArray());
        AddRole(addRoleIds.ToArray());
    }

    public void AddDataScope(params string[] organizationIds)
    {
        foreach (var organizationId in organizationIds)
        {
            if (UserDataScopes.Any(rd => rd.OrganizationId == organizationId))
            {
                continue;
            }

            var dataScope = new SysUserDataScope()
            {
                UserId = Id,
                OrganizationId = organizationId
            };
            UserDataScopes.Add(dataScope);
        }
    }

    public void RemoveDataScope(params string[] organizationIds)
    {
        foreach (var organizationId in organizationIds)
        {
            var dataScope = UserDataScopes.Where(rd => rd.OrganizationId == organizationId).FirstOrDefault();

            if (dataScope != null)
            {
                UserDataScopes.Remove(dataScope);
            }
        }
    }

    public void SetDataScope(params string[] organizationIds)
    {
        var oldOrganizationIds = UserDataScopes.Select(rd => rd.OrganizationId);

        var removeOrganizationIds = oldOrganizationIds.Except(organizationIds);
        var addOrganizationIds = organizationIds.Except(oldOrganizationIds);

        RemoveDataScope(removeOrganizationIds.ToArray());
        AddDataScope(addOrganizationIds.ToArray());
    }
}

